/*
 * @Description: 
 * @Date: 2022-02-18 11:24:02
 * @Author: caikaixuan
 * @LastEditTime: 2022-02-18 16:41:55
 */

const User = require('../models/module/User')
const { nanoid } = require('nanoid')
async function createUser(wx_openid){
    if(wx_openid == null){
        throw APIError.USER_WX_OPENID_EMPTY
    }
    let uuid = nanoid()
    let user = await User.create({
        wx_openid: wx_openid,
        uuid:uuid,
        is_black: false
    })
    if (user){
        return user
    }
    throw APIError.USER_CREATE_ERROR
}

async function findUserByOpenid(wx_openid){
    if(wx_openid == null){
        throw APIError.USER_WX_OPENID_EMPTY
    }
    let user = await User.findOne({
        where:{
            wx_openid:wx_openid
        }
    })
    if (user){
        return user
    }else{
        return null
    }
}

module.exports = {
    findUserByOpenid:findUserByOpenid,
    createUser: createUser
}